#include "viewer.h"
#include <osg/Camera>
#include "scene.h"
//#include <dtCore/keyboard.h>
//#include <dtCore/deltawin.h>
//#include <dtCore/keyboardmousehandler.h>
#include <cassert>
#include "drawable.h"

namespace CoreBase
{
	//class ViewImpl
	//{
	//public:
	//	ViewImpl(osgViewer::Viewer& view): _osgViewerView(&view),_renderOrder(0)
	//	{
	//		assert(_osgViewerView.valid());
	//		_camera = view.getCamera();
	//	}
	//	//KeyboardMouseHandler* GetKeyboardMouseHandler() { return mKeyboardMouseHandler.get(); }
	//	bool getMouseIntersections(osgUtil::LineSegmentIntersector::Intersections& hitlist,
	//		const osg::Vec2& mousepos,
	//		unsigned int traversalmask);
	//	typedef QVector<osg::ref_ptr<osg::Camera> > CameraSet;
	//	CameraSet _cameraSlave;
	//	osg::ref_ptr<osgViewer::Viewer> _osgViewerView;
	//	osg::ref_ptr<osg::Camera> _camera;
	//	osg::ref_ptr<Scene> _scene;
	//	//osg::ref_ptr<dtCore::KeyboardMouseHandler> mKeyboardMouseHandler;
	//	double _targetFrameRate;
	//	unsigned int _renderOrder;
	//	//osg::ref_ptr<DatabasePager> mPager;
	//};


	Viewer::Viewer(bool usescenelight):_setupScene(false)
	{
		ctor(usescenelight);
	}

	void Viewer::ctor(bool usescenelight)
	{
		if (usescenelight)
		{
			//dynamic_cast<osgViewer::View*>(this)->setLightingMode(osg::View::HEADLIGHT);//
		}
		else
		{
			//dynamic_cast<osgViewer::View*>(this)->setLightingMode(osg::View::NO_LIGHT);
		}
		//CreateKeyboardMouseHandler();
		//if (_osgViewerView->getDatabasePager() != NULL)
		//{
		//   SetDatabasePager(new DatabasePager(*_osgViewerView->getDatabasePager()));
		//}
		//else
		//{
		//   SetDatabasePager(new DatabasePager());
		//}
	}

	Viewer::~Viewer()
	{}

	void Viewer::setRenderOrder(unsigned int rordernum)
	{
		_renderOrder = rordernum;
		getCamera()->setRenderOrder(osg::Camera::NESTED_RENDER, rordernum);
	}

	unsigned int Viewer::getRenderOrder() const
	{
		return _renderOrder;
	}

	//bool Viewer::addSlave(osg::Camera* camera)
	//{
	//	if (camera == NULL)
	//	{
	//		throw;
	//	}
	//	_cameraSlave.push_back(camera);
	//	return addSlave(camera);
	//}

	//bool Viewer::removeSlave(osg::Camera* camera)
	//{
	//	assert(camera);
	//	_cameraSlave.remove(_cameraSlave.indexOf(camera));
	//	return true;
	//}

	void Viewer::setMainCamera(osg::Camera* camera)
	{
		if (getCamera() != camera)
		{
			if (camera!=NULL)
			{
				setCamera(camera);
				dynamic_cast<osgViewer::View*>(this)->assignSceneDataToCameras();
			}
		}
	}

	void Viewer::setScene(Scene* scene)
	{
		if (_scene != scene)
		{
			if (_scene.valid())
			{
				_scene->removeView(*this);
				//_scene->setDatabasePager(NULL);
			}
			_scene = scene;
			if (_scene.valid())
			{
				_scene->addView(*this);
				_setupScene = true;
				//_scene->SetDatabasePager(mPager.get());
				updateFromScene();
			}
		}
	}

	Scene* Viewer::getScene()
	{
		return (_scene.get());
	}

	const Scene* Viewer::getScene() const
	{
		return (_scene.get());
	}

	//void Viewer::SetMouse(Mouse* mouse)
	//{
	//   mKeyboardMouseHandler->SetMouse(mouse);
	//}

	//void Viewer::SetKeyboard(Keyboard* keyboard)
	//{
	//   mKeyboardMouseHandler->SetKeyboard(keyboard);
	//}

	//Keyboard* Viewer::GetKeyboard()
	//{
	//   return mKeyboardMouseHandler.valid() ? mKeyboardMouseHandler->GetKeyboard() : NULL;
	//}

	//const Keyboard* Viewer::GetKeyboard() const
	//{
	//   return mKeyboardMouseHandler.valid() ? mKeyboardMouseHandler->GetKeyboard() : NULL;
	//}

	//Mouse* Viewer::GetMouse()
	//{
	//   return mKeyboardMouseHandler.valid() ? mKeyboardMouseHandler->GetMouse() : NULL;
	//}

	//const Mouse* Viewer::GetMouse() const
	//{
	//   return mKeyboardMouseHandler.valid() ? mKeyboardMouseHandler->GetMouse() : NULL;
	//}

	void Viewer::updateFromScene()
	{
		dynamic_cast<osgViewer::View*>(this)->setSceneData(_scene->getSceneNode());
		dynamic_cast<osgViewer::View*>(this)->assignSceneDataToCameras();
	}

	//void Viewer::clearSlave()
	//{
	//	_cameraSlave.clear();
	//}

	//void Viewer::SetDatabasePager(DatabasePager* pager)
	//{
	//   if (mPager.get() != pager)
	//   {
	//      mPager = pager;
	//      if (mPager.valid())
	//      {
	//         _osgViewerView->setDatabasePager(mPager->GetOsgDatabasePager());
	//      }
	//      else
	//      {
	//         _osgViewerView->setDatabasePager(NULL);
	//      }
	//      if (_scene.valid())
	//      {
	//         _scene->SetDatabasePager(mPager.get());
	//      }
	//   }
	//}

	//DatabasePager* dtCore::Viewer::GetDatabasePager()
	//{
	//   return mPager.get();
	//}
	//
	//const DatabasePager* dtCore::Viewer::GetDatabasePager() const
	//{
	//   return mPager.get();
	//}
	//
	//KeyboardMouseHandler* Viewer::CreateKeyboardMouseHandler()
	//{
	//    mKeyboardMouseHandler = new dtCore::KeyboardMouseHandler(this);
	//    _osgViewerView->addEventHandler(mKeyboardMouseHandler.get());
	//    return mKeyboardMouseHandler.get();
	//}
}